package offer.algorithm;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * Description：
 * Author: zhangc
 * Date：2017/7/27 23:38
 */
public class LastRemaining45 {

    public static Integer lastRemaining(Integer[] array,int m){
        int legth = array.length;
        int distance = m-1;
        int start =0;
        List<Integer> list = new ArrayList<>(Arrays.asList(array));
        int index;
        for (int i = 0; i < legth-1; i++) {
            index = (start + distance) % list.size();
            if (index == 0 ||index == list.size()-1){
                start =0;
            }else start = index;
            list.remove(index);
        }
        return list.get(0);
    }

}
